package com.huadao.cart_ccs.experiment.log.protocolLog;

import com.huadao.cart_ccs.common.ProjectManager;
import com.huadao.cart_ccs.data.bo.ExperimentStage;
import com.huadao.cart_ccs.data.bo.Protocol;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.gen.ExperimentStageDao;
import com.huadao.cart_ccs.greendao.gen.ProtocolDao;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class ProtocolPresenter extends ProtocolLogContract.IProtocolLogPresenter {

    //    private ProtocolLogContract.IProtocolLogView baseView;
    private ProtocolDao protocolDao;
    private ExperimentStageDao experimentStageDao;
    private int skip;
    private int count=50;
    private int day=1;
    private long projectId;
    private long stageId=-1;
    private Disposable getData_dispose;

    public ProtocolPresenter(ProtocolLogContract.IProtocolLogView baseView) {
        this.baseView = baseView;
        attachView(baseView);
        protocolDao= GreenDaoManager.getInstance().getNoCacheSession().getProtocolDao();
//        startDate= CartApplication.getInstance().getProject().getStartTime();
//        if(startDate==null){
//            startDate=new Date();
//        }
//        startDate=new Date(startDate.getTime()+86400000*(day-1));
//        endDate=new Date(startDate.getTime()+86400000);
        projectId=ProjectManager.getInstance().getProjectId();

        experimentStageDao=GreenDaoManager.getInstance().getNoCacheSession().getExperimentStageDao();
    }

    @Override
    void getProtocolList() {
        getData_dispose=Observable
                .create(new ObservableOnSubscribe<List<Protocol>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<Protocol>> emitter) throws Exception {
                        skip=0;
                        List<Protocol> protocolList=protocolDao.queryBuilder().where(ProtocolDao.Properties.ProjectId.eq(projectId),ProtocolDao.Properties.StageId.eq(stageId)).orderDesc(ProtocolDao.Properties.Date).limit(count).list();
                        skip+=protocolList.size();
                        emitter.onNext(protocolList);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<Protocol>>() {
                    @Override
                    public void accept(List<Protocol> o) throws Exception {
                        baseView.showProtocolLogList(o);
                    }
                });

//        skip=0;
//        List<Protocol> protocolList=protocolDao.queryBuilder().where(ProtocolDao.Properties.ProjectId.eq(projectId),ProtocolDao.Properties.StageId.eq(stageId)).orderDesc(ProtocolDao.Properties.Date).limit(count).list();
//        skip+=protocolList.size();
//        baseView.showProtocolLogList(protocolList);
    }

    @Override
    public void getMoreProtocols() {
        getData_dispose=Observable
                .create(new ObservableOnSubscribe<List<Protocol>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<Protocol>> emitter) throws Exception {
                        List<Protocol> protocolList=protocolDao.queryBuilder().where(ProtocolDao.Properties.ProjectId.eq(projectId),ProtocolDao.Properties.StageId.eq(stageId)).orderDesc(ProtocolDao.Properties.Date).offset(skip).limit(count).list();
                        skip+=protocolList.size();
                        emitter.onNext(protocolList);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<Protocol>>() {
                    @Override
                    public void accept(List<Protocol> o) throws Exception {
                        baseView.showMoreProtocolLogList(o);
                    }
                });

//        List<Protocol> protocolList=protocolDao.queryBuilder().where(ProtocolDao.Properties.ProjectId.eq(projectId),ProtocolDao.Properties.StageId.eq(stageId)).orderDesc(ProtocolDao.Properties.Date).offset(skip).limit(count).list();
//        skip+=protocolList.size();
//        baseView.showMoreProtocolLogList(protocolList);
    }

    @Override
    void setDay(int day) {
        this.day=day;
        initStageId();
    }

    @Override
    int getDay() {
        return day;
    }

    private void initStageId(){
        List<ExperimentStage> experimentStageList=experimentStageDao.queryBuilder().where(ExperimentStageDao.Properties.ProjectId.eq(projectId),ExperimentStageDao.Properties.Day.eq(day)).list();
        if(experimentStageList!=null&&experimentStageList.size()>0) {
            stageId=experimentStageList.get(0).getId();
        }else {
            stageId=-1;
        }
    }

    @Override
    public void detachView() {
        if(getData_dispose!=null){
            getData_dispose.dispose();
        }
        super.detachView();
    }
}
